package edu.sysu.cs;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class ScoreRecorder {
	class RecordComparator implements Comparator<Record> {
		@Override
		public int compare(Record o1, Record o2) {
			if (o1.win < o2.win)
				return 1;
			else {
				return 0;
			}
		}
	}

	class Record {
		@Override
		public String toString() {
			return String.format("%-10s%-4d%-4d%-3d%-4d%-1s", name, win, lose,
					tie, undo, "\n");
		}

		public Record() {

		}

		public Record(String name, int win, int lose, int tie, int undo) {
			super();
			this.name = name;
			this.win = win;
			this.lose = lose;
			this.tie = tie;
			this.undo = undo;
		}

		public String name;
		public int win;
		public int lose;
		public int tie;
		public int undo;
	}

	ArrayList<Record> records = new ArrayList<Record>();
	String fileName = "score.txt";
	BufferedReader reader = null;
	FileWriter writer = null;

	public ScoreRecorder() {
		try {
			reader = new BufferedReader(new FileReader(fileName));
			String tmp;
			while ((tmp = reader.readLine()) != null) {
				if (!tmp.equals("") && tmp != null) {
					String[] rd = tmp.split(",");
					if (rd.length == 5)
						records.add(new Record(rd[0], Integer.valueOf(rd[1]),
								Integer.valueOf(rd[2]), Integer.valueOf(rd[3]),
								Integer.valueOf(rd[4])));
				}

			}
			if (reader != null) {
				reader.close();
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 
	 * @param player
	 *            name of player
	 * @param winlose
	 *            1=win 2=lose 3=tie
	 */
	private int containUser(String player) {
		for (int i = 0; i < records.size(); i++) {
			if (records.get(i).name.equals(player))
				return i;
		}
		return -1;
	}

	/**
	 * @param player
	 *            name of player
	 * @param winlose
	 *            1=win 2=lose 3=tie 4=undo
	 */
	public void setRecord(String player, int winlose) {
		if (player == null) {
			player = System.getProperty("user.name");
		}
		player = player.toLowerCase();
		if (containUser(player) >= 0) {
			if (winlose == 1) {
				records.get(containUser(player)).win++;
			} else if (winlose == 2) {
				records.get(containUser(player)).lose++;
			} else if (winlose == 3) {
				records.get(containUser(player)).tie++;
			} else if (winlose == 4) {
				records.get(containUser(player)).undo++;
			}

		} else {
			int win = winlose == 1 ? 1 : 0;
			int lose = winlose == 2 ? 1 : 0;
			int tie = winlose == 3 ? 1 : 0;
			int undo = winlose == 4 ? 1 : 0;
			records.add(new Record(player, win, lose, tie,undo));
		}
		Collections.sort(records, new RecordComparator());
		refreshFile();

	}

	public String getRecord() {
		StringBuilder tmp = new StringBuilder("Player    Win Lose Tie Undo\n");
		for (Record rd : records) {
			tmp.append(rd);
		}
		return tmp.toString();
	}

	public void refreshFile() {
		try {
			writer = new FileWriter(fileName, false);
			for (Record rd : records) {
				StringBuilder sb = new StringBuilder();
				sb.append(rd.name).append(",").append(rd.win).append(",")
						.append(rd.lose).append(",").append(rd.tie).append(",").append(rd.undo)
						.append("\n");
				writer.write(sb.toString());
			}
			if (writer != null) {
				writer.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	@Override
	protected void finalize() throws Throwable {
		refreshFile();
		super.finalize();
	}
}